package example.springboot.controller;

import example.springboot.common.Result;
import example.springboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
@RestController
@RequestMapping("/api/plan")
@CrossOrigin
public class PlanApiController {

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 创建学习计划
     */
    @PostMapping
    public Result<Map<String, Object>> createPlan(
            HttpServletRequest request,
            @RequestBody Map<String, Object> data) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

            Integer targetWords = (Integer) data.get("targetWords");
            String deadline = (String) data.get("deadline");

            if (targetWords == null || targetWords <= 0) {
                return Result.error("目标单词数必须大于0");
            }
            if (deadline == null || deadline.isEmpty()) {
                return Result.error("截止日期不能为空");
            }

          
            
            Map<String, Object> plan = new HashMap<>();
            plan.put("id", 1);
            plan.put("targetWords", targetWords);
            plan.put("deadline", deadline);
            plan.put("progress", 0);
            
            return Result.success("创建成功", plan);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建学习计划失败：" + e.getMessage());
        }
    }

    /**
     * 查看学习计划
     */
    @GetMapping
    public Result<Map<String, Object>> getPlan(HttpServletRequest request) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

        
            
            Map<String, Object> plan = new HashMap<>();
            plan.put("id", 1);
            plan.put("targetWords", 100);
            plan.put("finishedWords", 0);
            plan.put("deadline", "2025-12-31");
            plan.put("status", "in_progress");
            
            return Result.success(plan);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取学习计划失败：" + e.getMessage());
        }
    }

    /**
     * 更新学习进度
     */
    @PutMapping("/{id}")
    public Result<String> updateProgress(
            HttpServletRequest request,
            @PathVariable Integer id,
            @RequestBody Map<String, Object> data) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

            Integer progress = (Integer) data.get("progress");
            if (progress == null) {
                return Result.error("进度不能为空");
            }

         
            
            return Result.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新学习进度失败：" + e.getMessage());
        }
    }

    /**
     * 删除学习计划
     */
    @DeleteMapping("/{id}")
    public Result<String> deletePlan(
            HttpServletRequest request,
            @PathVariable Integer id) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

         
            
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除学习计划失败：" + e.getMessage());
        }
    }

    /**
     * 从请求中获取用户ID
     */
    private Integer getUserIdFromRequest(HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            
            if (token == null || token.isEmpty()) {
                return null;
            }

            return jwtUtil.getUserIdFromToken(token);
        } catch (Exception e) {
            return null;
        }
    }
}

